<template>
  <div class="default-main ba-table-box">
    <TableHeader
      :buttons="[
        'refresh',
        'add',
        'edit',
        'remove',
        'comSearch',
        'columnDisplay',
      ]"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 表格 -->
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" @action="baTable.onTableAction" />
    <!-- 表单 -->
    <PopupForm v-if="baTable.form.operate" ref="formRef" />
  </div>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, markRaw } from "vue";
import { useRouter, useRoute } from "vue-router";
import baTableClass from "@/utils/baTable";
import { LineCruisePlan } from "@/api/backend/index";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import PopupForm from "./popupForm.vue";
import { useStaff } from "@/store/backend/ServiceM/staff";
import { useStandard } from "@/store/backend/LineCruiseM/standard";
import DoTime from "./components/doTime.vue";
import { useNavTabs } from "@/store/navTabs";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const staff = useStaff();
const standard = useStandard();
const navTabs = useNavTabs();
const route = useRoute();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + LineCruisePlan)
      ? (navTabs as any).pageSizeMap.get(route.path + LineCruisePlan)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(LineCruisePlan),
  {
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("planNumber"),
        label: "计划编号",
        prop: "planNumber",
        align: "center",
      },
      {
        show: filedShowOff("planName"),
        label: "计划名称",
        prop: "planName",
        align: "center",
      },
      {
        show: filedShowOff("cycle"),
        label: "循环周期",
        prop: "cycle",
        align: "center",
        render: "customTemplate",
        customTemplate: (row: TableRow, field: TableColumn, value: any) => {
          return row.unit == "day"
            ? value + "天"
            : row.unit == "hour"
            ? value + "时"
            : row.unit == "week"
            ? value + "周"
            : row.unit == "month"
            ? value + "月"
            : row.unit == "quarter"
            ? value + "季"
            : value + "年";
        },
      },
      {
        show: filedShowOff("reminderTime"),
        label: "提前通知时间(分钟)",
        prop: "reminderTime",
        align: "center",
      },
      {
        label: "执行时间",
        prop: "",
        align: "center",
        render: "customRender",
        customRender: markRaw(DoTime),
        operator: false,
      },
      {
        show: filedShowOff("stopTime"),
        label: "计划停用时间",
        prop: "stopTime",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("employeeName"),
        label: "巡检人员名称",
        prop: "employeeName",
        align: "center",
      },
      {
        // show: operatorShowOff([route.path + "/remove", route.path + "/edit"]),
        label: "操作",
        align: "center",
        width: "130",
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "fa fa-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确认删除？",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
        operator: false,
      },
    ],
    filter: {
      // id: Local.get("adminInfo")?.companyid,
      // regions: [] + "",
      // name: route.query.name,
      pageSize: pageSizeKeep(),
    },
    acceptQuery: false,
  },
  {
    defaultItems: {
      // status: 0,
    },
  }
);

onMounted(() => {
  //全局事件总线
  const { proxy } = useCurrentInstance();
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(LineCruisePlan);
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("staff", staff);
provide("standard", standard);
provide("navTabs", navTabs);
</script>
<style lang="scss" scoped></style>
